package borge.web.teamspirit.client.data;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.jdo.annotations.Extension;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.NotPersistent;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import borge.web.teamspirit.client.TeamSpirit;
import borge.web.teamspirit.client.interfaces.PersistentData;
import borge.web.teamspirit.client.interfaces.UserContentProvider;
import borge.web.teamspirit.client.servercomm.DBQuery;

/**
 * 
 * Each registered user will have a Person entity.
 * Note that a Person does not belong to any team.
 * 
 * Once a Person join a team, a TeamPlayer entity will be 
 * created that maps this person to that team.
 * 
 * That way, a Person can belong to many teams and have different
 * player profiles in each team.
 * 
 */
@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable="true")
public class Person implements PersistentData {

	private static final long serialVersionUID = -6562445835290017545L;

	// These must be identical to corresponding object fields!
	public static final String KEY_USERNAME = "iUserName";
	public static final String KEY_PASSWORD = "iPassword";
	

	@PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    @Extension(vendorName="datanucleus", key="gae.encoded-pk", value="true")
    private String iEncodedKey;
	
	// Not database entity
	@NotPersistent
	private String iSessionId;
    
	@Persistent
	private String iUserName;
	
    @Persistent
    private String iPassword;
	
	// Personal
	@Persistent
	private String iFirstName;
	
	@Persistent
	private String iLastName;
	
	@Persistent    
	private String iNickName;
	
	@Persistent
	private Date iBirthDay;
	
	@Persistent
	private String iPhone;
	
	@Persistent
	private String iEmail;
	
	@Persistent
	private List<String> iOwnedTeamNames;
	
	@Persistent
	private List<String> iTeamNames;
	
	@Persistent
	private List<String> iAssociationNames;

	
	public Person(){
		
	}
	
	@Override
	public String toString(){
		return iFirstName + " " + iLastName;
	}
	
	public String getEncodedKey(){
		return iEncodedKey;
	}
	
	public String getSessionId(){
		return iSessionId;
	}
	
	public void setSessionId(String aSessionId){
		iSessionId = aSessionId;
	}

	public void setUserName(String aUserName){
		iUserName = aUserName;
	}
	
	public String getUserName(){
		return iUserName;
	}
	
    public void setPassword(String aPassword) {
        this.iPassword = aPassword;
    }

    public String getPassword() {
        return iPassword;
    }

    // for test purposes only
    public void setName(String aName){
    	
    	int space = aName.indexOf(' ');
    	String fname = aName.substring(0, space);
    	String lname = aName.substring(space+1, aName.length());
    	setFirstName(fname);
    	setLastName(lname);
    }
    
    public void setFirstName(String aFirstName) {
        this.iFirstName = aFirstName;
    }

    public String getFirstName() {
        return iFirstName;
    }

    public void setLastName(String aLastName) {
        this.iLastName = aLastName;
    }

    public String getLastName() {
        return iLastName;
    }

    public void setNickName(String aNickName) {
        this.iNickName = aNickName;
    }

    public String getNickName() {
        return iNickName;
    }

    public void setBirthDay(Date aBirthDay) {
        this.iBirthDay = aBirthDay;
    }

    public Date getBirthDay() {
        return iBirthDay;
    }

    public void setPhone(String aPhone) {
        this.iPhone = aPhone;
    }

    public String getPhone() {
        return iPhone;
    }

    public void setEmail(String aEmail) {
        this.iEmail = aEmail;
    }

    public String getEmail() {
        return iEmail;
    }
    
    public void addOwnedTeam(String aTeam){
    	if(iOwnedTeamNames == null)
    		iOwnedTeamNames = new ArrayList<String>();
    	
    	iOwnedTeamNames.add(aTeam);
    }
    
    public List<String> getOwnedTeams(){
    	return iOwnedTeamNames;
    }

    public void addTeamName(String aTeam){
    	if(iTeamNames == null)
    		iTeamNames = new ArrayList<String>();
    	
    	iTeamNames.add(aTeam);
    }
    
    public List<String> getTeamNames(){
    	return iTeamNames;
    }
    
    public void addAssociationName(String aAssName){
    	if(iAssociationNames == null)
    		iAssociationNames = new ArrayList<String>();
    }
    
    public List<String> getAssociationNames(){
    	return iAssociationNames;
    }
	
	@Override
	public String getDisplayingWidget() {
		// TODO Auto-generated method stub
		return null;
	}
		
	public static Person createPersonFromUserForm(Person aPerson, UserContentProvider aProvider){
	    
	    if(aPerson == null)
	        aPerson = new Person(); 
	    
	    aPerson.setUserName(aProvider.getString(TeamSpirit.getConstants().username()));
	    aPerson.setPassword(aProvider.getString(TeamSpirit.getConstants().password()));
	    
	    aPerson.setFirstName(aProvider.getString(TeamSpirit.getConstants().first_name()));
	    aPerson.setLastName(aProvider.getString(TeamSpirit.getConstants().last_name()));
	    aPerson.setNickName(aProvider.getString(TeamSpirit.getConstants().nick_name()));
	    aPerson.setBirthDay(aProvider.getDate(TeamSpirit.getConstants().day_of_birth()));
	    aPerson.setPhone(aProvider.getString(TeamSpirit.getConstants().phone()));
	    aPerson.setEmail(aProvider.getString(TeamSpirit.getConstants().email()));
	    
	    
	    return aPerson;
	}
	
	public List<DBQuery> getRelationsQueries(boolean associations, boolean reggedTeams, boolean ownedTeams){
		
		List<DBQuery> queries = new ArrayList<DBQuery>();
		
		/*
		if(associations){
			DBQuery query = new DBQuery(DataKey.TEAM, DBQuery.SELECT, TeamPlayer.class.getName());
			query.addFilter("iTeamKey", DBQuery.EQUALS, "teamParam", "String", iEncodedKey, null);
			query.addOrdering("iPersonName", DBQuery.ASCENDING);
			queries.add(query);
		}
		if(players){
			DBQuery query = new DBQuery(DataKey.TEAMPLAYER, DBQuery.SELECT, TeamPlayer.class.getName());
			query.addFilter("iTeamKey", DBQuery.EQUALS, "teamParam", "String", iEncodedKey, null);
			query.addOrdering("iPersonName", DBQuery.ASCENDING);
			queries.add(query);
		}*/
		
		return queries;
		
	}

	public String dumpContent(){
		return new String("Dump: " + "\nId:" + iEncodedKey + "\nFirst name:"
				+ iFirstName + "\nLast name:" + iLastName + "\nNickname:"
				+ iNickName + "\nBirthday:" + iBirthDay + "\nPhone:" + iPhone
				+ "\nEmail:" + iEmail);
	}
	
}
